package com.winit.attachment.service;

import com.winit.attachment.repository.AttachmentRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
public class AttachmentService implements IAttachmentService {

    private static Logger log = LoggerFactory.getLogger(AttachmentService.class);

    @Autowired
    private AttachmentRepository attachmentRepository;

    @Override
    public int flushRelationToOwner(String ownerId, List<String> attachmentIds, int operationMode) {
        int updateNum = 0;
        if (2 == operationMode || 3 == operationMode){    //删除所有者时或者更新所有者时
            updateNum = this.attachmentRepository.updateIsDelOfAttachmentByOwnerId(ownerId, "0");
        }
        if (1 == operationMode || 3 == operationMode){  //新增或者修改所有者时
            if (CollectionUtils.isEmpty(attachmentIds)){
                return updateNum;
            }
            updateNum = this.attachmentRepository.updateOwnerRelation(ownerId, attachmentIds, "1");
        }

        return updateNum;
    }

    @Override
    public int deleteAttachmentByBrdId(String brdId, String isDel) {
        return this.attachmentRepository.updateIsDelOfAttachmentByOwnerId(brdId, isDel);
    }
}
